The present invention relates to a compile method, a compiler, an exception handler, and a program recording medium, which are particularly associated with translation into a machine language such as a programming language by which exception processing in a computer can be efficiently dealt with or associated with efficient dealing of a program thus translated.
A compiler is a piece of software by which a source program (or source file) written in a programming language and relating to a program used in a computer is translated into a machine language, thereby to prepare an object (or object file).
There are various programming languages, and languages capable of processing an exception in accordance with an instruction from a user in the level of a user program have come to be used in recent years. Exception processing means processing which responds when undesirable processing occurs while executing the program. For example, processing with regard to processing of dividing a certain variable by 0. Such exceptions include use of an array area deviating from an area to be properly used, a null-pointer (0-address reference), an Input/Output (I/O) error, and the like.
Thus, in a programming language capable of efficiently processing exceptions, it is possible to specify a particular range of a program and to recognize an exception occurring within the range, to control the flow of the program. For example, it is supposed that program format is written as follows.
______________________________________ try { sentence 1, 1; sentence 1, 2; . . . sentence 1, N; } catch (equation 2) { sentence 2, 1; . . . } catch (equation 3) { . . . } . . . ______________________________________
In this case, if an exception corresponding to an equation 2 occurs within a range surrounded by "try" (which will be hereinafter called a try section) and including sentences 1.1, 1.2, . . . , 1.N, the flow of the program goes to a section surrounded by "catch" corresponding thereto (which will be hereinafter called a catch section) and including a sentence 2.1, . . . , . This means that an exception occurring in the try section is processed in the catch section.
The processing described above will be schematically expressed as in FIG. 1.
FIG. 1 is a diagram showing the flow of processing when an exception occurs during execution of a program.
At first, when an exception 83 occurs during execution of a try section 82 in a program 81, the flow of the processing is shifted to an exception handler 84. The exception handler 84 determines which of a plurality of catch sections 85 (#1, #2, . . . , #N) the exception corresponds to, and the processing flow is shifted to the corresponding catch section 85 thus determined. The exception handler 84 is a program for allocating exception processing. Although the exception handler 84 is provided in a kernel level in an OS (operating system) in some cases, it is here supposed that the handler 84 is previously prepared as an object in a library and is linked with an object as a program body by a linker.
Supposing that a program for processing the exception 83 is provided in the catch section #2 and the flow of the processing is shifted to the catch section #2 by the exception handler 84. The catch section #2 executes predetermined exception processing and shifts the processing to a predetermined destination 86 after exception processing.
The series of processing as described above ensures the operation from occurrence of an exception to completion of exception processing and to recovery of normal processing.
A program constitutes a nest structure which starts from a main loop such as a main entry and then calls other objects and functions. In other words, processing is carried out hierarchically, and other objects and functions are read starting from a main loop.
The following will explain how exception processing is carried out in this case.
For example, if a current procedure is a called function and if occurrence of an exception does not have a corresponding catch section, the flow immediately returns from a currently executed procedure and exception processing is continued assuming that an exception occurs at the location where the flow returns.
FIG. 2 is a diagram showing a program example for explaining processing in a case where an exception occurs in a calling function.
In the figure, it is supposed that an exception has occurred in a sentence 1.1 in a try section in a FUNC 3 called from a FUNC 1. In this time, if there is no catch section corresponding to the exception, the processing flow returns to the position immediately after a FUNC 3 in the FUNC 1 is called. The processing flow goes again to an exception handler to check whether or not there is corresponding exception processing in the FUNC 1. In this case, if the exception corresponds to the catch section (equation 4) in FUNC 1, the flow of the program is shifted to the sentence 4.1.
FIG. 3 shows a nest structure in a case where functions are hierarchically called and processing is carried out.
For example, in case of including a nest loop as shown in the figure, if an exception 95 occurs in a function 94, the exception handler 84 searches for a catch section for executing corresponding exception processing in the function 94. If no corresponding exception processing is found, the function 94 is returned to a return point 96 of a function 93. In the function 93, the processing is shifted to the exception handler 84 and the similar operation is sequentially repeated. If no corresponding catch section is found in the functions 94, 93, and 92, the program is finally returned to a function 91. In this process, the processing is shifted from every one of return points 96, 97, and 98.
A compiler must translate a source file into such a machine language in which a mechanism as described above operates properly. Therefore, the compiler maintains a table (which will be hereinafter referred to as an exception information table) describing a range surrounded by "try" (from the top of a sentence 1.1 to the end of a sentence 1.N), kinds of exceptions (equations 1, 2, . . . ), and positions (tops of sentences 2.1, 3.1, and the like) to which control is shifted when the exceptions occur. If an exception occurs, the compiler generates machine language command lines.
The exception handler serves to check the exception information table and to perform processing of shifting the flow of the program to a corresponding catch section.
Note that the structure of the exception information table consists of "start position at try section", "end position at try section", start position of corresponding catch section", and "kind of exception".
If an exception occurs in the exception processing method described above, variables defined until the time immediately before occurrence of the exception are referred to in a catch section or in a section after the catch section, in several cases. Therefore, the values of those variables may not be broken in the processing by the exception handler.
There are two methods to realize this.
In one method, values of all the registers to be used by an exception handler are put aside into a memory at the top of an exception handler and the values of the registers thus put aside are written back into the registers when the flow returns from the exception handler (e.g., when the flow enters into a catch section).
In the other method, when compiling a program, variables which are live at the entry of a catch section are allocated not to registers but to a memory. A variable x which is live at a time point means a possibility that the contents of the variable x may be referred to after the time point. Such a variable x is called a live variable, i.e., the other variables than the live variable x are dead variables.
Compiling is conventionally carried out in accordance with either of the two methods described above. In case where compiling is carried out in accordance with the former method, there are problems that the processing by the exception handler is slow, or that, when no corresponding catch section is found and the flow returns from a procedure, processing for recovering original states of registers broken by the procedure is carried out, and therefore, information of the registers to be broken by the procedure must be held in any way. Meanwhile, in case where compiling is carried out in accordance with the latter method, there is a problem that allocation of variables to registers is restricted due to exception processing, and therefore, the execution efficiency is lowered even in the processing to be carried out when no exception occurs.
In addition, the following problems are involved by a compiler of a program language capable of processing an exception as instructed by a user within the level of a user program.
Since the flow of a program is changed by occurrence of an exception, commands are not rearranged by optimization during compiling, before and after a section where an exception may occur. If the layout is changed, values of variables living at the entry of a catch section cannot be guaranteed.
Further, where in a program sequence in a try section an exception will occur can be determined only during execution of a program. Therefore, when compiling a program, it is necessary to check whether or not an exception has occurred, at every one of the portions where an exception can occur. For example, when the flow returns from a procedure as in the case of in FIG. 3, it is necessary to check whether or not an exception has occurred in a procedure called. If an exception has occurred, it is necessary to generate an exception information table and a command line for transmitting the current position to an exception handler. This is a factor which increases the program execution period where no exception has occurred.